package com.offcn.service.impl;

import com.offcn.common.CreateNumberCommon;
import com.offcn.mapper.DepartmentMapper;
import com.offcn.mapper.RoleMemberMapper;
import com.offcn.pojo.*;
import com.offcn.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    DepartmentMapper departmentMapper;

    @Autowired
    RoleMemberMapper roleMemberMapper;

    @Override
    public List<Department> selectDepartmentByMemberSession(Member memberSession) {
        RoleMemberExample exp1 = new RoleMemberExample();
        RoleMemberExample.Criteria criteria = exp1.createCriteria();
        criteria.andMemberidEqualTo(memberSession.getMid());
        List<RoleMember> roleMembers = roleMemberMapper.selectByExample(exp1);
        Long roleid = roleMembers.get(0).getRoleid();

        if (roleid == 1L) { //经理   添加所有部门

            DepartmentExample exp2 = new DepartmentExample();
            List<Department> departments = departmentMapper.selectByExample(exp2);
            return departments;
        }
        if (roleid == 2L) {//部长    添加自己部门员工
            ArrayList<Department> departments = new ArrayList<>();
            Integer departmentid = memberSession.getDepartmentid();
            Department department = departmentMapper.selectByPrimaryKey(departmentid);
            return departments;
        }
        //员工无此功能
        return null;
    }

    @Override
    public boolean save(Member memberSession,String dname) {
        RoleMemberExample exp1 = new RoleMemberExample();
        RoleMemberExample.Criteria criteria = exp1.createCriteria();
        criteria.andMemberidEqualTo(memberSession.getMid());
        List<RoleMember> roleMembers = roleMemberMapper.selectByExample(exp1);
        Long roleid = roleMembers.get(0).getRoleid();

        if (roleid == 1L) {
            Department department = new Department();
            int createId = CreateNumberCommon.getCreateId();
            department.setDid(createId);
            department.setDname(dname);
            int flag = departmentMapper.insert(department);
            return  flag>0;
        }
        return false;
    }

    @Override
    public boolean deleteByDepartment(Member memberSession,Integer did) {
        RoleMemberExample exp1 = new RoleMemberExample();
        RoleMemberExample.Criteria criteria = exp1.createCriteria();
        criteria.andMemberidEqualTo(memberSession.getMid());
        List<RoleMember> roleMembers = roleMemberMapper.selectByExample(exp1);
        Long roleid = roleMembers.get(0).getRoleid();
        if(roleid==1L){

            int flag = departmentMapper.deleteByPrimaryKey(did);

            return flag>0;


        }

        return false;
    }

    @Override
    public boolean update(Member memberSession,Department department) {
        RoleMemberExample exp1 = new RoleMemberExample();
        RoleMemberExample.Criteria criteria = exp1.createCriteria();
        criteria.andMemberidEqualTo(memberSession.getMid());
        List<RoleMember> roleMembers = roleMemberMapper.selectByExample(exp1);
        Long roleid = roleMembers.get(0).getRoleid();
        if(roleid==1L) {

            departmentMapper.updateByPrimaryKey(department);
        }
        return false;
    }
}